package cn.net.yato.picture.mapper;

import cn.net.yato.picture.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName UserMapper
 * @Description TODO
 * @Author yato
 * @Date 2020/2/17 14:41
 * @Version 1.0
 */
@Component
public interface UserMapper extends BaseMapper<User> {

    /**
     * 查询所有用户信息
     * @return
     */
    @Select("SELECT  `user`.id,`user`.user_id,`user`.username,`user`.nickname,`user`.`password`," +
            "`user`.salt,`user`.email,`user`.phone,`user`.sex,`user`.age,`user`.address,`user`.birthday," +
            "`user`.`status`,`user`.create_time,`user`.update_time,`user`.last_login_time,`user`.wecatId," +
            "`user`.userHeadImgId  FROM  `user`")
    @Results(id = "userResult", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userName", column = "username"),
            @Result(property = "roles", column = "user_id",many = @Many(select = "cn.net.yato.picture.mapper.RoleMapper.getRolesByUserId")),
            @Result(property = "permissions", column = "user_id",many = @Many(select = "cn.net.yato.picture.mapper.PermissionMapper.getPermissionsByUserId")),
            @Result(property = "userHeadImg", column = "userHeadImgId",one = @One(select = "cn.net.yato.picture.mapper.UserHeadImgMapper.getUserHeadImgByFileId"))
    })
    List<User> getAllUsers();

    /**
     * 用户名登录/根据用户名查询用户信息
     * @param userName
     * @return
     */
    @Select("SELECT  `user`.id,`user`.user_id,`user`.username,`user`.nickname,`user`.`password`," +
            "`user`.salt,`user`.email,`user`.phone,`user`.sex,`user`.age,`user`.address,`user`.birthday," +
            "`user`.`status`,`user`.create_time,`user`.update_time,`user`.last_login_time,`user`.wecatId," +
            "`user`.userHeadImgId  FROM  `user`  WHERE  `user`.username = #{userName}")
    @ResultMap(value = "userResult")
    User loginByUserName(@Param(value = "userName") String userName);


}
